{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e792290a",
   "metadata": {},
   "source": [
    "# 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "83b81991",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>sex</th>\n",
       "      <th>bmi</th>\n",
       "      <th>children</th>\n",
       "      <th>smoker</th>\n",
       "      <th>region</th>\n",
       "      <th>charges</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19</td>\n",
       "      <td>female</td>\n",
       "      <td>27.900</td>\n",
       "      <td>0</td>\n",
       "      <td>yes</td>\n",
       "      <td>southwest</td>\n",
       "      <td>16884.92400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>male</td>\n",
       "      <td>33.770</td>\n",
       "      <td>1</td>\n",
       "      <td>no</td>\n",
       "      <td>southeast</td>\n",
       "      <td>1725.55230</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>male</td>\n",
       "      <td>33.000</td>\n",
       "      <td>3</td>\n",
       "      <td>no</td>\n",
       "      <td>southeast</td>\n",
       "      <td>4449.46200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>33</td>\n",
       "      <td>male</td>\n",
       "      <td>22.705</td>\n",
       "      <td>0</td>\n",
       "      <td>no</td>\n",
       "      <td>northwest</td>\n",
       "      <td>21984.47061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>32</td>\n",
       "      <td>male</td>\n",
       "      <td>28.880</td>\n",
       "      <td>0</td>\n",
       "      <td>no</td>\n",
       "      <td>northwest</td>\n",
       "      <td>3866.85520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>31</td>\n",
       "      <td>female</td>\n",
       "      <td>25.740</td>\n",
       "      <td>0</td>\n",
       "      <td>no</td>\n",
       "      <td>southeast</td>\n",
       "      <td>3756.62160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age     sex     bmi  children smoker     region      charges\n",
       "0   19  female  27.900         0    yes  southwest  16884.92400\n",
       "1   18    male  33.770         1     no  southeast   1725.55230\n",
       "2   28    male  33.000         3     no  southeast   4449.46200\n",
       "3   33    male  22.705         0     no  northwest  21984.47061\n",
       "4   32    male  28.880         0     no  northwest   3866.85520\n",
       "5   31  female  25.740         0     no  southeast   3756.62160"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "data = pd.read_csv('./data/insurance.csv',sep=',')\n",
    "data.head(n=6)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3838a24",
   "metadata": {},
   "source": [
    "读取CSV的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ad9c232a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 'female', 27.9, ..., 'yes', 'southwest', 16884.924],\n",
       "       [18, 'male', 33.77, ..., 'no', 'southeast', 1725.5523],\n",
       "       [28, 'male', 33.0, ..., 'no', 'southeast', 4449.462],\n",
       "       ...,\n",
       "       [18, 'female', 36.85, ..., 'no', 'southeast', 1629.8335],\n",
       "       [21, 'female', 25.8, ..., 'no', 'southwest', 2007.945],\n",
       "       [61, 'female', 29.07, ..., 'yes', 'northwest', 29141.3603]],\n",
       "      dtype=object)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.values"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75dbe933",
   "metadata": {},
   "source": [
    "读取data的index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b4daf5f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=1338, step=1)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b42669e",
   "metadata": {},
   "source": [
    "列出data的类别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "46dfbae4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'sex', 'bmi', 'children', 'smoker', 'region', 'charges'], dtype='object')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "f5a06f33",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       19\n",
       "1       18\n",
       "2       28\n",
       "3       33\n",
       "4       32\n",
       "        ..\n",
       "1333    50\n",
       "1334    18\n",
       "1335    18\n",
       "1336    21\n",
       "1337    61\n",
       "Name: age, Length: 1338, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['age']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "295693b1",
   "metadata": {},
   "source": [
    "# EDA 数据探索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f3db8a80",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92631604",
   "metadata": {},
   "source": [
    "绘制直方图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4d86773d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([536., 398., 129.,  86.,  35.,  59.,  57.,  32.,   2.,   4.]),\n",
       " array([ 1121.8739  ,  7386.729311, 13651.584722, 19916.440133,\n",
       "        26181.295544, 32446.150955, 38711.006366, 44975.861777,\n",
       "        51240.717188, 57505.572599, 63770.42801 ]),\n",
       " <BarContainer object of 10 artists>)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAP3UlEQVR4nO3dfczdZX3H8fdnlAenjvLQNU1bd2MkM5hMYHcYRGMcxA2KEf5Qg1lmw7o0mSzRuMSVmWwz2R/gkqFkC9qIW1lUYKijQTdlFTOXRbDIM8i4wRLaAK0IOGdchn73x7mKp+Xu/dD73A/nyvuVnJzr9/1d5/y+P/vz08N1HpqqQpLUl19a7gYkSaNnuEtShwx3SeqQ4S5JHTLcJalDq5a7AYBTTz21JiYmlrsNSRord9999w+qas10+1ZEuE9MTLB79+7lbkOSxkqSJ4+0z2UZSeqQ4S5JHTLcJalDhrskdchwl6QOGe6S1CHDXZI6ZLhLUocMd0nq0Ir4hupCTGz7yrIde89VFy/bsSVpJr5yl6QOGe6S1CHDXZI6ZLhLUocMd0nqkOEuSR0y3CWpQ4a7JHXIcJekDs0p3JPsSfJAknuT7G61k5PcnuSxdn9SqyfJtUmmktyf5OzFPAFJ0ivN55X7b1f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wMXAVckedvwzhV+Ha1isLx6XVWdBfwPg2WYl63w/mc0DuE+Lj9n8GySdQDtfn+rH6n/meobpqmPVJJjGQT756rqS+N4DgdV1QvAHQyWIlYnOfjlvOHjvtxr238i8BzzP7dReQvwriR7gBsZLM18coz6B6Cq9rX7/cCXGfwlOy7X0V5gb1Xd2bZvYRD249L/zJZq/WcB62KrGLxBcRq/eGPoTSugrwkOXXP/aw59E+bjbXwxh74Jc1ern8xgve+kdvs+cHLbd/ibMJtG3HuAG4BPHFYfp3NYA6xu41cB3wLeCfwTh74h+YE2voJD35C8uY3fxKFvSD7B4M3IJbvugLfzizdUx6Z/4NXAa4fG/wlcOGbX0beAX2/jv2y9j03/M57bUh1ogX8Amxh8ouNx4KMroJ8vAE8D/8fgb/8tDNY/dwGPAf829IcbBv94yePAA8Dk0PP8ATDVbpcP1SeBB9tj/pbD3vAZQf9vZfCfmvcD97bbpjE7h98A7mnn8CDw563++vZ/qCkGQXl8q5/Qtqfa/tcPPddHW5+PMvRphqW67jg03Mem/9brfe320MFjjNl1dCawu11H/8wgnMem/5lu/vyAJHVoHNbcJUnzZLhLUocMd0nqkOEuSR0y3CWpQ4a7JHXIcJekDv0/90q0R57s+kMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(data['charges'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e0bf6dd4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 19.,  27.,  52.,  53.,  47.,  54.,  71.,  84.,  90., 108., 126.,\n",
       "        157.,  98.,  58.,  67.,  56.,  35.,  80.,  50.,   6.]),\n",
       " array([ 7.02275569,  7.22477015,  7.42678461,  7.62879907,  7.83081352,\n",
       "         8.03282798,  8.23484244,  8.4368569 ,  8.63887136,  8.84088581,\n",
       "         9.04290027,  9.24491473,  9.44692919,  9.64894365,  9.8509581 ,\n",
       "        10.05297256, 10.25498702, 10.45700148, 10.65901594, 10.86103039,\n",
       "        11.06304485]),\n",
       " <BarContainer object of 20 artists>)"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD4CAYAAAAXUaZHAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAASbElEQVR4nO3df7BndX3f8eerbACxU1ncK8VddDcRTdCJI7klJE4cks0oSoalrXWWJs1q6OzYYGp+TM2azIRpZ5xCmqmJSWtnK9S1Y1FKbaBRo4RoaGcK9kJEdkHCCiK7XdhrUKy1g6Lv/vE9m3775V7uvd/z/X7v3Q/Px8zO95zP+Zzvec/h+PLcz/f8SFUhSWrLX1vvAiRJk2e4S1KDDHdJapDhLkkNMtwlqUGb1rsAgC1bttT27dvXuwxJOqncddddX62quaWWbYhw3759OwsLC+tdhiSdVJI8stwyh2UkqUGGuyQ1aMVwT3J9kuNJDo60/1KSLyY5lOS3h9rfneRwkgeSvGEaRUuSnt1qxtw/CPwB8KETDUl+EtgFvLqqnkryoq79fGA38ErgxcCfJHl5VX130oVLkpa34pl7Vd0OPDHS/I+Aa6rqqa7P8a59F/CRqnqqqh4GDgMXTrBeSdIqjDvm/nLgJ5LcmeTPkvytrn0r8OhQvyNd2zMk2ZtkIcnC4uLimGVIkpYybrhvAs4CLgL+CXBjkqzlC6pqf1XNV9X83NySl2lKksY0brgfAT5WA58DvgdsAY4C5w7129a1SZJmaNxw/0PgJwGSvBw4FfgqcAuwO8lpSXYA5wGfm0CdkqQ1WPFqmSQ3ABcDW5IcAa4Grgeu7y6P/DawpwZv/TiU5EbgPuBp4CqvlNFz2fZ9Hx973S9fc+kEK9FzzYrhXlVX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+Ay7rlpzMYRjoMfA74/g1Q0z8f2lefAX5wRvvqvzL4H/g9wM6NcFytsq6ZHFsMTlyOAd9hMBZ8JYPfZ24DHmRwJc9ZXd954AND6/5Cd4wdBt623jUx+Gvx3m6f3QtcOYN99be76aeAx4FPdX1fDHxiaN03Mfjr7EvAb06qJh8/IEkNOpmGZSRJq2S4S1KDDHdJapDhLkkNMtwlqUGGuyQ1yHCXpAb9X/dl2L3VxTFzAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.hist(np.log(data['charges']),bins=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10ab6bb7",
   "metadata": {},
   "source": [
    "特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bfe13214",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>bmi</th>\n",
       "      <th>children</th>\n",
       "      <th>charges</th>\n",
       "      <th>sex_female</th>\n",
       "      <th>sex_male</th>\n",
       "      <th>smoker_no</th>\n",
       "      <th>smoker_yes</th>\n",
       "      <th>region_northeast</th>\n",
       "      <th>region_northwest</th>\n",
       "      <th>region_southeast</th>\n",
       "      <th>region_southwest</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19</td>\n",
       "      <td>27.900</td>\n",
       "      <td>0</td>\n",
       "      <td>16884.92400</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>33.770</td>\n",
       "      <td>1</td>\n",
       "      <td>1725.55230</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>33.000</td>\n",
       "      <td>3</td>\n",
       "      <td>4449.46200</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>33</td>\n",
       "      <td>22.705</td>\n",
       "      <td>0</td>\n",
       "      <td>21984.47061</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>32</td>\n",
       "      <td>28.880</td>\n",
       "      <td>0</td>\n",
       "      <td>3866.85520</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age     bmi  children      charges  sex_female  sex_male  smoker_no  \\\n",
       "0   19  27.900         0  16884.92400           1         0          0   \n",
       "1   18  33.770         1   1725.55230           0         1          1   \n",
       "2   28  33.000         3   4449.46200           0         1          1   \n",
       "3   33  22.705         0  21984.47061           0         1          1   \n",
       "4   32  28.880         0   3866.85520           0         1          1   \n",
       "\n",
       "   smoker_yes  region_northeast  region_northwest  region_southeast  \\\n",
       "0           1                 0                 0                 0   \n",
       "1           0                 0                 0                 1   \n",
       "2           0                 0                 0                 1   \n",
       "3           0                 0                 1                 0   \n",
       "4           0                 0                 1                 0   \n",
       "\n",
       "   region_southwest  \n",
       "0                 1  \n",
       "1                 0  \n",
       "2                 0  \n",
       "3                 0  \n",
       "4                 0  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=pd.get_dummies(data)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d2a6ea1",
   "metadata": {},
   "source": [
    "创建数据集X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f95fac9d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>bmi</th>\n",
       "      <th>children</th>\n",
       "      <th>sex_female</th>\n",
       "      <th>sex_male</th>\n",
       "      <th>smoker_no</th>\n",
       "      <th>smoker_yes</th>\n",
       "      <th>region_northeast</th>\n",
       "      <th>region_northwest</th>\n",
       "      <th>region_southeast</th>\n",
       "      <th>region_southwest</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>19</td>\n",
       "      <td>27.900</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>18</td>\n",
       "      <td>33.770</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>33.000</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>33</td>\n",
       "      <td>22.705</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>32</td>\n",
       "      <td>28.880</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1333</th>\n",
       "      <td>50</td>\n",
       "      <td>30.970</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1334</th>\n",
       "      <td>18</td>\n",
       "      <td>31.920</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1335</th>\n",
       "      <td>18</td>\n",
       "      <td>36.850</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1336</th>\n",
       "      <td>21</td>\n",
       "      <td>25.800</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1337</th>\n",
       "      <td>61</td>\n",
       "      <td>29.070</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1338 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      age     bmi  children  sex_female  sex_male  smoker_no  smoker_yes  \\\n",
       "0      19  27.900         0           1         0          0           1   \n",
       "1      18  33.770         1           0         1          1           0   \n",
       "2      28  33.000         3           0         1          1           0   \n",
       "3      33  22.705         0           0         1          1           0   \n",
       "4      32  28.880         0           0         1          1           0   \n",
       "...   ...     ...       ...         ...       ...        ...         ...   \n",
       "1333   50  30.970         3           0         1          1           0   \n",
       "1334   18  31.920         0           1         0          1           0   \n",
       "1335   18  36.850         0           1         0          1           0   \n",
       "1336   21  25.800         0           1         0          1           0   \n",
       "1337   61  29.070         0           1         0          0           1   \n",
       "\n",
       "      region_northeast  region_northwest  region_southeast  region_southwest  \n",
       "0                    0                 0                 0                 1  \n",
       "1                    0                 0                 1                 0  \n",
       "2                    0                 0                 1                 0  \n",
       "3                    0                 1                 0                 0  \n",
       "4                    0                 1                 0                 0  \n",
       "...                ...               ...               ...               ...  \n",
       "1333                 0                 1                 0                 0  \n",
       "1334                 1                 0                 0                 0  \n",
       "1335                 0                 0                 1                 0  \n",
       "1336                 0                 0                 0                 1  \n",
       "1337                 0                 1                 0                 0  \n",
       "\n",
       "[1338 rows x 11 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X=data.drop('charges',axis=1)\n",
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0fdcbb8",
   "metadata": {},
   "source": [
    "创建数据集Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "49a6c1cf",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       16884.92400\n",
       "1        1725.55230\n",
       "2        4449.46200\n",
       "3       21984.47061\n",
       "4        3866.85520\n",
       "           ...     \n",
       "1333    10600.54830\n",
       "1334     2205.98080\n",
       "1335     1629.83350\n",
       "1336     2007.94500\n",
       "1337    29141.36030\n",
       "Name: charges, Length: 1338, dtype: float64"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = data['charges']\n",
    "Y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cb740a3",
   "metadata": {},
   "source": [
    "如果X,Y中有空值（NULL）则按0来填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "b9b445fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "X.fillna(0,inplace=True)\n",
    "Y.fillna(0,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f2662fe",
   "metadata": {},
   "source": [
    "划分训练集和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "78df6245",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>bmi</th>\n",
       "      <th>children</th>\n",
       "      <th>sex_female</th>\n",
       "      <th>sex_male</th>\n",
       "      <th>smoker_no</th>\n",
       "      <th>smoker_yes</th>\n",
       "      <th>region_northeast</th>\n",
       "      <th>region_northwest</th>\n",
       "      <th>region_southeast</th>\n",
       "      <th>region_southwest</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>558</th>\n",
       "      <td>35</td>\n",
       "      <td>34.105</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>56</td>\n",
       "      <td>39.820</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>887</th>\n",
       "      <td>36</td>\n",
       "      <td>30.020</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>62</td>\n",
       "      <td>26.290</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>383</th>\n",
       "      <td>35</td>\n",
       "      <td>43.340</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>266</th>\n",
       "      <td>40</td>\n",
       "      <td>19.800</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>460</th>\n",
       "      <td>49</td>\n",
       "      <td>36.630</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>489</th>\n",
       "      <td>53</td>\n",
       "      <td>31.160</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>170</th>\n",
       "      <td>63</td>\n",
       "      <td>41.470</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>94</th>\n",
       "      <td>64</td>\n",
       "      <td>31.300</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>936 rows × 11 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     age     bmi  children  sex_female  sex_male  smoker_no  smoker_yes  \\\n",
       "558   35  34.105         3           1         0          0           1   \n",
       "13    56  39.820         0           1         0          1           0   \n",
       "887   36  30.020         0           1         0          1           0   \n",
       "11    62  26.290         0           1         0          0           1   \n",
       "383   35  43.340         2           1         0          1           0   \n",
       "..   ...     ...       ...         ...       ...        ...         ...   \n",
       "266   40  19.800         1           0         1          0           1   \n",
       "460   49  36.630         3           1         0          1           0   \n",
       "489   53  31.160         1           0         1          1           0   \n",
       "170   63  41.470         0           0         1          1           0   \n",
       "94    64  31.300         2           1         0          0           1   \n",
       "\n",
       "     region_northeast  region_northwest  region_southeast  region_southwest  \n",
       "558                 0                 1                 0                 0  \n",
       "13                  0                 0                 1                 0  \n",
       "887                 0                 1                 0                 0  \n",
       "11                  0                 0                 1                 0  \n",
       "383                 0                 0                 1                 0  \n",
       "..                ...               ...               ...               ...  \n",
       "266                 0                 0                 1                 0  \n",
       "460                 0                 0                 1                 0  \n",
       "489                 0                 1                 0                 0  \n",
       "170                 0                 0                 1                 0  \n",
       "94                  0                 0                 0                 1  \n",
       "\n",
       "[936 rows x 11 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.3)\n",
    "X_train"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b645618",
   "metadata": {},
   "source": [
    "百战写的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df620def",
   "metadata": {},
   "source": [
    "归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "100ce111",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "scaler = StandardScaler(with_mean=True,with_std=True).fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3f81ee98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.3216816 ,  0.53399181,  1.58858932, ...,  1.85933936,\n",
       "        -0.6284369 , -0.56418956],\n",
       "       [ 1.1909915 ,  1.46604074, -0.92876892, ..., -0.53782543,\n",
       "         1.59124966, -0.56418956],\n",
       "       [-0.24964955, -0.13222339, -0.92876892, ...,  1.85933936,\n",
       "        -0.6284369 , -0.56418956],\n",
       "       ...,\n",
       "       [ 0.97489534,  0.05369713, -0.08964951, ...,  1.85933936,\n",
       "        -0.6284369 , -0.56418956],\n",
       "       [ 1.69521587,  1.73513623, -0.92876892, ..., -0.53782543,\n",
       "         1.59124966, -0.56418956],\n",
       "       [ 1.76724792,  0.07652948,  0.7494699 , ..., -0.53782543,\n",
       "        -0.6284369 ,  1.77245393]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train_scaled=scaler.transform(X_train)\n",
    "x_test_scaled = scaler.transform(X_test)\n",
    "x_train_scaled"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73cf097f",
   "metadata": {},
   "source": [
    "多项式回归（升维）（因为不知道该数据是否是线性的，需要升维）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "2893ca04",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "poly_Features = PolynomialFeatures(degree = 2,include_bias=False)\n",
    "x_train_scaled = poly_Features.fit_transform(x_train_scaled)\n",
    "x_test_scaled = poly_Features.fit_transform(x_test_scaled)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e85d7f34",
   "metadata": {},
   "source": [
    "# 模型训练"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6d01404",
   "metadata": {},
   "source": [
    "不带正则项的多元线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "889ea88f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import Ridge\n",
    "\n",
    "reg = LinearRegression()\n",
    "reg.fit(x_train_scaled,np.log1p(Y_train))\n",
    "y_predict = reg.predict(x_test_scaled)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b5c6832",
   "metadata": {},
   "source": [
    "带正则项的多元线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "f0a56292",
   "metadata": {},
   "outputs": [],
   "source": [
    "ridge_reg = Ridge(alpha=10)\n",
    "ridge_reg.fit(x_train_scaled,np.log1p(Y_train))\n",
    "y_predict_ridge = ridge_reg.predict(x_test_scaled)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d97e5be2",
   "metadata": {},
   "source": [
    "# 模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "d18786ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(Y_train),y_pred=reg.predict(x_train_scaled)))\n",
    "# 训练集y真实值和y预测值进行误差分析RMSE(对MSE开根号)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "261de4d1",
   "metadata": {},
   "source": [
    "对np.log1p(Y_test)评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "9d0fdb16",
   "metadata": {},
   "outputs": [],
   "source": [
    "log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(Y_test),y_pred=y_predict))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "871a1514",
   "metadata": {},
   "source": [
    "对Y_train,Y_test不做np.log1p()正态分布处理时"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e55a6a5e",
   "metadata": {},
   "source": [
    "对Y_train评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "12e7f025",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "rmse_train=np.sqrt(mean_squared_error(y_true=Y_train,y_pred=np.exp(reg.predict(x_train_scaled))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d565745a",
   "metadata": {},
   "source": [
    "对Y_test评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "dda693a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "rmse_test=np.sqrt(mean_squared_error(y_true=Y_test,y_pred=np.exp(y_predict)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "43008ea0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "log_rmse_train:0.34542578753985276\n",
      " log_rmse_test:0.4141325267642783\n",
      " rmse_train:5102.20767706679\n",
      " rmse_test:5465.623737626529\n"
     ]
    }
   ],
   "source": [
    "print(f\"log_rmse_train:{log_rmse_train}\\n log_rmse_test:{log_rmse_test}\\n rmse_train:{rmse_train}\\n rmse_test:{rmse_test}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55425aee",
   "metadata": {},
   "source": [
    "# 正则化处理后的模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "c2c2dcde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.3418488882378784, 0.4162372113168367, 4842.27989178851, 5389.085017359511)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ridge_log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(Y_train),y_pred=ridge_reg.predict(x_train_scaled)))\n",
    "ridge_log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(Y_test),y_pred=y_predict_ridge))\n",
    "ridge_rmse_train=np.sqrt(mean_squared_error(y_true=Y_train,y_pred=np.exp(ridge_reg.predict(x_train_scaled))))\n",
    "ridge_rmse_test=np.sqrt(mean_squared_error(y_true=Y_test,y_pred=np.exp(y_predict_ridge)))\n",
    "ridge_log_rmse_train,ridge_log_rmse_test,ridge_rmse_train,ridge_rmse_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36fdd782",
   "metadata": {},
   "source": [
    "# 用GradientBoostingRegressor进行非线性拟合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "f28139fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import GradientBoostingRegressor\n",
    "Grad_reg = GradientBoostingRegressor()\n",
    "Grad_reg.fit(x_train_scaled,np.log1p(Y_train))\n",
    "y_predict_Grad = Grad_reg.predict(x_test_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "ac697511",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.24845795725157402, 0.4147988772752863, 3522.966552332688, 4968.669847368691)"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Grad_log_rmse_train=np.sqrt(mean_squared_error(y_true=np.log1p(Y_train),y_pred=Grad_reg.predict(x_train_scaled)))\n",
    "Grad_log_rmse_test=np.sqrt(mean_squared_error(y_true=np.log1p(Y_test),y_pred=y_predict_Grad))\n",
    "Grad_rmse_train=np.sqrt(mean_squared_error(y_true=Y_train,y_pred=np.exp(Grad_reg.predict(x_train_scaled))))\n",
    "Grad_rmse_test=np.sqrt(mean_squared_error(y_true=Y_test,y_pred=np.exp(y_predict_Grad)))\n",
    "Grad_log_rmse_train,Grad_log_rmse_test,Grad_rmse_train,Grad_rmse_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a1101f90",
   "metadata": {},
   "source": [
    "自己写的。。。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59aad83e",
   "metadata": {},
   "source": [
    "数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "b56c80d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa50487b",
   "metadata": {},
   "source": [
    "标准归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "40224c05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.49773336, -0.51489371, -0.07880937, ..., -0.57570539,\n",
       "        -0.60532913, -0.57077056],\n",
       "       [-0.42234334,  0.48530711,  3.23649725, ..., -0.57570539,\n",
       "         1.65199385, -0.57077056],\n",
       "       [-0.78080668, -0.49186277,  0.75001728, ..., -0.57570539,\n",
       "        -0.60532913,  1.75201747],\n",
       "       ...,\n",
       "       [ 1.44166602, -0.4556713 ,  1.57884394, ..., -0.57570539,\n",
       "         1.65199385, -0.57077056],\n",
       "       [-1.4260407 ,  1.50113607, -0.07880937, ...,  1.73699953,\n",
       "        -0.60532913, -0.57077056],\n",
       "       [ 0.72473934,  0.88341336, -0.90763603, ..., -0.57570539,\n",
       "         1.65199385, -0.57077056]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scaler = StandardScaler()\n",
    "scaler.fit(X_train)\n",
    "X_train = scaler.transform(X_train)\n",
    "X_train"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfaba715",
   "metadata": {},
   "source": [
    "需要对测试集进行相同的数据预处理流程，而且所使用的均值和方差均来自当前训练集的均值和方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "07b30f70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.58505136, -0.37670807, -0.90763603, ..., -0.57570539,\n",
       "        -0.60532913,  1.75201747],\n",
       "       [ 1.58505136, -1.39006944, -0.90763603, ..., -0.57570539,\n",
       "        -0.60532913, -0.57077056],\n",
       "       [ 0.72473934, -0.09293399, -0.90763603, ...,  1.73699953,\n",
       "        -0.60532913, -0.57077056],\n",
       "       ...,\n",
       "       [-0.70911402,  0.82419094,  0.75001728, ..., -0.57570539,\n",
       "        -0.60532913,  1.75201747],\n",
       "       [ 1.51335869, -1.06187854, -0.90763603, ...,  1.73699953,\n",
       "        -0.60532913, -0.57077056],\n",
       "       [ 1.58505136,  0.96977939, -0.07880937, ..., -0.57570539,\n",
       "        -0.60532913, -0.57077056]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test = scaler.transform(X_test)\n",
    "X_test"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d13c952a",
   "metadata": {},
   "source": [
    "多项式升维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c8707949",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import PolynomialFeatures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "15d29639",
   "metadata": {},
   "outputs": [],
   "source": [
    "poly_features = PolynomialFeatures(degree=2,include_bias=True)\n",
    "X_poly_train = poly_features.fit_transform(X_train)\n",
    "X_poly_test = poly_features.fit_transform(X_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "67eda543",
   "metadata": {},
   "source": [
    "训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "80c32b16",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import ElasticNet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "e989b4f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "elastic_reg = ElasticNet(alpha=0.1,l1_ratio=0.2,max_iter=300000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "f6c3872b",
   "metadata": {},
   "outputs": [],
   "source": [
    "elastic_reg.fit(X_poly_train,np.log1p(Y_train))\n",
    "Y_train_predict = elastic_reg.predict(X_poly_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c417548",
   "metadata": {},
   "source": [
    "测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1e071a32",
   "metadata": {},
   "outputs": [],
   "source": [
    "Y_test_predict = elastic_reg.predict(X_poly_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5252a15a",
   "metadata": {},
   "source": [
    "求解损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "41177bd5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "680dd259",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "degree_mse_train: 0.15396404061806196\n",
      "degree_mse_test: 0.13433057024309358\n"
     ]
    }
   ],
   "source": [
    "print(f\"degree_mse_train:\",mean_squared_error(np.log1p(Y_train),Y_train_predict))\n",
    "print(f\"degree_mse_test:\", mean_squared_error(np.log1p(Y_test), Y_test_predict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "585d2bfc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "759616c9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b8a844c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_test",
   "language": "python",
   "name": "my_test"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
